<template>
  <div class="calendar">
    <el-date-picker v-model="from.date" type="month" placeholder="选择月">
    </el-date-picker>
    <el-input v-model="input" placeholder="请输入内容"></el-input>
    <el-button
      type="success"
      @click="addtext"
      icon="el-icon-check"
      circle
    ></el-button>
    <el-calendar v-model="from.date">
      <template slot="dateCell" slot-scope="{ data, date }">
        <div
          :class="{
            day_box: true,
            is_active: data.day == day,
            is_hui: from.date.getMonth() != date.getMonth(),
          }"
          @click.stop="
            from.date.getMonth() == date.getMonth() ? clickDay(data) : () => {}
          "
        >
          <p :class="{ 'is-selected': data.isSelected, date: true }">
            {{ date.getDate() }} <br />
            日
          </p>
          <div class="content">
            <el-tag
              size="mini"
              v-for="(item, index) of list[data.day] || []"
              :key="index"
              @click.stop="clickText({ id: data.day, val: item })"
              >{{ item }}</el-tag
            >
          </div>
        </div>
      </template>
    </el-calendar>
  </div>
</template>

<script>
import moment from "moment";
import { getData } from "@/apis";
export default {
  naem: "calendar",
  data() {
    return {
      from: {
        date: new Date(),
      },
      input: "",
      day: "",
      list: {},
    };
  },
  created() {
    getData(5).then((res) => {
      console.log(res);
    });
  },
  watch: {
    "from.date": {
      handler(val) {
        if (val) this.day = moment(val).format("YYYY-MM-DD");
      },
      immediate: true,
    },
  },
  methods: {
    clickDay(val) {
      console.log(val);
      this.day = val.day;
    },
    addtext() {
      this.$set(this.list, this.day, [
        ...(this.list[this.day] || []),
        this.input,
      ]);
    },
    clickText(data) {
      console.log(data);
    },
  },
};
</script>

<style scoped lang="less">
.calendar {
  & /deep/ .el-calendar-table .el-calendar-day {
    padding: 0;
  }
  .day_box {
    position: relative;
    height: 100%;
    background-color: #fff;
    /* padding: 8px; */
  }
  .day_box.is_active {
    background-color: #f2f8fe;
  }
  .day_box.is_hui {
    background-color: rgb(245, 245, 245);
  }
  .date {
    /* color: #1989fa; */
    position: absolute;
    right: 0;
    top: 0;
    margin: 4px;
    text-align: right;
  }
}
</style>